#include "RegPolygon.h"

RegPolygon::RegPolygon() : Region() //Default is a triangle
{
	sides = 3;
	circumradius = 1;
}

RegPolygon::RegPolygon(int edges, double crad) : Region()
{
	sides = edges;
	circumradius = crad;
}

double RegPolygon::getApothemLength()
{
	return circumradius*cos(M_PI/sides);
}

double RegPolygon::getSideLength()
{
	return 2*circumradius*sin(M_PI/sides);
}

double RegPolygon::getInteriorAngle() //In radians
{
	return ((sides - 2)*M_PI)/sides;
}

/*For simulation purposes, one vertex is always held horizontally from the center of the polygon, at the 0 angle.
The center of the polygon is given as the origin for X,Y coordinates.
Orientation of the regular polygon does not truly matter for an expected exit time calculation. */

bool RegPolygon::pointInside(double *position,double error)
{
	double sum=0;
	//Positions of two vertices of interest
	double v1[2];
	double v2[2];
	//Find all angles between the particle point and vertices in radians.
	double* angles = new double[sides];
	for(unsigned int i=0;i<sides;i++)
	{
		v1[0]=circumradius*cos((2*M_PI*i)/sides)-position[0];
		v1[1]=circumradius*sin((2*M_PI*i)/sides)-position[1];
		v2[0]=circumradius*cos((2*M_PI*(i+1))/sides)-position[0];
		v2[1]=circumradius*sin((2*M_PI*(i+1))/sides)-position[1];
		angles[i]= acos((v1[0]*v2[0]+v1[1]*v2[1])/(sqrt(pow(v1[0],2)+pow(v1[1],2))*sqrt(pow(v2[0],2)+pow(v2[1],2))));
	}
	//Sum all of the angles
	for(unsigned int i=0;i<sides;i++)
	{
		sum += angles[i];
	}
	delete angles;
	if(abs(sum-2*M_PI)<error)
	{
		return true;
	}
	else
	{
		return false;
	}
}